
function grid2pos(x,y){
	return cc.p(x*GRID_SIZE, y*GRID_SIZE);
}
function faceAngle(pos, newPos){
	var y = newPos.y - pos.y;
	var x = newPos.x - pos.x;
	var angle =  Math.atan2(x,y) * (180/Math.PI);
	return angle;
}

function scn(S){
	cc.Director.getInstance().replaceScene(new S());
}

function createGrid(h,w){
	var x = Math.floor(Math.random()*GRID_INFO.x) * GRID_INFO.width;
	var y = Math.floor(Math.random()*GRID_INFO.y) * GRID_INFO.width;
	var grid = cc.Sprite.create(R_ROOM_GRIDBG, cc.rect(x,y,GRID_INFO.width, GRID_INFO.width));
	grid.setScale(0.95);
	grid.setAnchorPoint(cc.PointZero());
	grid.setPosition(grid2pos(w,h));
	
	var batch = TW.gameScn.bgLayer.batch;
	batch.addChild(grid);
	return grid;
}

function adjustSizeForGrid(sp){
	var sz = sp.getTextureRect().size;
	sp.setScale(GRID_SIZE/sz.width, GRID_SIZE/sz.height);
}

function createRoom(h,w){
	TW.room_h = h;
	TW.room_w = w;
	TW.grids = new Array(h);
	for(var i=0; i<TW.grids.length; ++i){
		TW.grids[i] = new Array(w);
	}
	
	var batch = TW.gameScn.bgLayer.batch;
	batch.removeAllChildren(true);
	
	for(var hi=0; hi<h; ++hi){
		for(var wi=0; wi<w; ++wi){
			createGrid(hi,wi);
		}
	}
}

function cardRect(c){
	var x = (c-1) % CARD_INFO.x;
	var y = Math.floor((c-1) / CARD_INFO.x);
	return cc.rect(x*CARD_INFO.width, y*CARD_INFO.width, CARD_INFO.width, CARD_INFO.width);
}
function createCard(w, c){
	if(0==c){
		return;
	}
	

	var card = cc.Sprite.create(R_ALL_CARD, cardRect(c));
	card.setAnchorPoint(cc.p(0.5,0.5));
	card.setScale(0.8);
	card.setPosition(grid2pos(w,TW.room_h));
	card.setOpacity(0);
	
	var batch = TW.gameScn.cardLayer.batch;
	batch.addChild(card);
	
	card.cid = c;
	TW.grids[TW.room_h-1][w] = card;
	card.runAction(cc.MoveTo.create(0.5, grid2pos(w, TW.room_h-1)));
	card.runAction(cc.FadeIn.create(0.6));
	return card;
}

function moveCard(h,w){
	var card = TW.grids[h][w];
	if(!card){
		return;
	}
	
	TW.grids[h][w] = null;
	if(h>0){
		TW.grids[h-1][w] = card;
		card.runAction(cc.MoveTo.create(0.5, grid2pos(w,h-1)));
	}
	else{
		card.removeFromParent(true);
	}
}

function addCardRow(cards){
	for(var h=0; h<TW.room_h; ++h){
		for(var w=0; w<TW.room_w; ++w){
			moveCard(h,w);
		}
	}
	
	cards.forEach(function(c,w){
		createCard(w,c);
	});
}

function createTank(player){
	var tank = cc.Node.create();
	tank.body = cc.Sprite.create(R_DEFAULT_TANKBODY);
	tank.root = tank.body;
	tank.addChild(tank.body);
	tank.hat = cc.Sprite.create(R_DEFAULT_TANKHAT);
	tank.addChild(tank.hat);
	
	tank.setAnchorPoint(cc.p(0.5,0.5));
	adjustSizeForGrid(tank.body);
	adjustSizeForGrid(tank.hat);
	
	tank.shade = cc.Node.create();
	tank.shade.addChild(tank);	
	bindTankUI(tank, player);
	tank.shade.setPosition(grid2pos(player.pos.x, player.pos.y));

	var p = TW.gameScn.tankLayer.tank;
	p.addChild(tank.shade);
	
	TW.tanks = TW.tanks || {};
	var oldTank = TW.tanks[player.id];
	if(oldTank){
		oldTank.shade.removeFromParent(true);
	}
	TW.tanks[player.id] = tank;
	tank.player = player;
	player.maxHP = player.hp;
	
	tank.getPositionShade = function(){
		return this.shade.getPosition();
	}
	
	updateHPBoard(tank.player.name, 100);
	
	return tank;
}

function bindTankUI(tank, player) {
    var hp = cc.ProgressTimer.create(cc.Sprite.create(R_HP_STATUS));
    hp.setType(cc.PROGRESS_TIMER_TYPE_BAR);
    hp.setMidpoint(cc.p(0, 0));
    hp.setBarChangeRate(cc.p(1, 0));
    hp.setPercentage(100);
    hp.setPosition(cc.p(0, 42));
    hp.setColor(cc.c3b(0, 255, 0));
	hp.setScaleY(0.3);
    tank.shade.addChild(hp);
    tank.hpBar = hp;
	
    var name = cc.LabelTTF.create(player.name, S_NAME_FONT, 24);
	if(player.self){
		name.setString(S_SELF_NAME);
	}
    name.setPosition(cc.p(0, 60));
	name.setColor(cc.c3b(0,43,61));
    tank.shade.addChild(name);	
}

function addTankAction(tank, act, num){
	tank.logicActs = tank.logicActs || [];
	var add;
	if(num===0){
		add = cc.RepeatForever.create(act);
	}
	else if(num>1){		
		add = cc.Repeat.create(act,num);
	}
	else{
		add = act;
	}
	tank.logicActs.push(add);
}

function getTankRunAction(tank){
	if(!tank.logicRun){
		tank.logicRun = function(){
			if(!tank.logicActs){
				return;
			}
			if(tank.logicActs.length==1){
				tank.runAction(tank.logicActs[0]);
			}
			else{
				tank.runAction(cc.Sequence.create(tank.logicActs));
			}
			delete tank.logicActs;
		}
	}
	return tank.logicRun;
}

function moveTank(id,x,y){
	var tank = TW.tanks[id];
	if(!tank){
		return;
	}
	
	var oldPos = tank.player.pos;
	if(oldPos.x==x && oldPos.y==y){
		return;
	}
	tank.player.pos = {x:x, y:y};
	tank.root.runAction(cc.RepeatForever.create(cc.Animate.create(loadAnim(R_MOVE0_ANIM))));
	
	var pos = tank.getPositionShade();
	var newPos = grid2pos(x,y);
	var angle =  faceAngle(pos, newPos);
	if(tank.getRotation() != angle){
		addTankAction(tank,cc.RotateTo.create(0.2, angle));
	}
	addTankAction(tank, cc.TargetedAction.create(tank.shade, cc.MoveTo.create(1.5, newPos)));
	addTankAction(tank, cc.CallFunc.create(function(){tank.root.stopAllActions();}));
	
	if(tank.hat.getRotation()!=0){
		tank.hat.runAction(cc.RotateTo.create(0.2,0));
	}
	return getTankRunAction(tank);
}

function faceTank(id,x,y,hat){
	var tank = TW.tanks[id];
	if(!tank){
		return;
	}
	
	var angle = faceAngle(tank.getPositionShade(), grid2pos(x,y));
	if(hat){
		angle = angle - tank.getRotation();
		if(tank.hat.getRotation()!=angle){
			addTankAction(tank, cc.TargetedAction.create(tank.hat,cc.RotateTo.create(0.2,angle)));
		}
	}
	else{
		if(tank.getRotation()!=angle){
			tank.root.runAction(cc.RepeatForever.create(cc.Animate.create(loadAnim(R_MOVE0_ANIM))));
			addTankAction(tank,cc.RotateTo.create(0.3, angle));
			addTankAction(tank,cc.CallFunc.create(function(){tank.root.stopAllActions();}));
		}	
		if(tank.hat.getRotation()!=0){
			tank.hat.runAction(cc.RotateTo.create(0.2, 0));
		}
	}
	return getTankRunAction(tank);
}

function tankShoot(i,x,y,noexp){
	faceTank(i,x,y,true);
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	var bullet = cc.Sprite.create(R_SHOOT_BULLET);
	var angle = faceAngle(tank.getPositionShade(), grid2pos(x,y));
	bullet.setRotation(angle);
	bullet.setScale(0.3,1);
	bullet.setPosition(tank.getPositionShade());
	bullet.setOpacity(0);
	var parent = TW.gameScn.tankLayer.effect;
	parent.addChild(bullet);
	
	var show = cc.FadeIn.create(0.1);
	var mov = cc.MoveTo.create(0.2, grid2pos(x,y));
	addTankAction(tank, cc.TargetedAction.create(bullet, cc.Spawn.create(mov,show)));
	addTankAction(tank, cc.CallFunc.create(function(){
		if(!noexp){
			var v1 = tank.getPositionShade();
			var v_d = calcVecAndDist(v1, grid2pos(x,y));
			v_d.d -= 10;
			v_d.v.x *= v_d.d;
			v_d.v.y *= v_d.d;
			createSmallExp(v1.x+v_d.v.x, v1.y+v_d.v.y);
		}
		bullet.removeFromParent(true);
	}));
	return getTankRunAction(tank);
}

function tankBullet(i,x,y){
	faceTank(i,x,y,false);
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	addTankAction(tank, cc.CallFunc.create(function(){
		var bullet = cc.ParticleSystem.createWithTotalParticles(100)
		bullet.setTexture(cc.TextureCache.getInstance().addImage(R_BULLET_STAR));
		bullet.setPosition(0,60);
		bullet.setAutoRemoveOnFinish(true);
		bullet.setDuration(0.3);
		bullet.setBlendAdditive(false);
		
		bullet.setEndSize(-1);
		bullet.setStartSize(10);
		bullet.setStartSizeVar(4);
		bullet.setAngle(90);
		bullet.setAngleVar(20);
		bullet.setLife(0.4);
		bullet.setEndColor(cc.c4f(0.75,0.25,0,1));
		bullet.setEndColorVar(cc.c4f(0, 0, 0, 0));
		bullet.setStartColor(cc.c4f(1,1,0,1));
		bullet.setStartColorVar(cc.c4f(0, 0, 0, 0));
		bullet.setRadialAccel(300);
		bullet.setSpeed(90);
		bullet.setSpeedVar(20);
		bullet.setEmissionRate(100);
		tank.addChild(bullet,100);
	}));
	
	return getTankRunAction(tank);
}

function calcVecAndDist(v1, v2){
	var v = cc.p(v2.x-v1.x, v2.y-v1.y);
	var dist = Math.sqrt(v.x*v.x + v.y*v.y);
	v.x /= dist;
	v.y /= dist;
	return {v:v, d:dist};
}

function createLaser(i, x, y){	
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	faceTank(i,x,y);
	addTankAction(tank, cc.CallFunc.create(function(){
		var laser = cc.ParticleSystem.createWithTotalParticles(250);
		laser.setTexture(cc.TextureCache.getInstance().addImage(R_LASER_PIC));
		laser.setPosition(0,60);
		laser.setAutoRemoveOnFinish(true);
		laser.setDuration(1.8);
		laser.setBlendAdditive(false);
		
		laser.setAngle(90);
		laser.setStartSize(25);
		laser.setEndSize(30);
		laser.setLife(4);
		laser.setStartColor(cc.c4f(1, 1, 1, 1));
		laser.setStartColorVar(cc.c4f(0, 0, 0, 0));
		laser.setEndColor(cc.c4f(0.8,0.8,0.8,1));
		laser.setEndColorVar(cc.c4f(0, 0, 0, 0));
		laser.setSpeed(300);
		laser.setRadialAccel(200);
		laser.setEndSpinVar(60);
		laser.setEmissionRate(40);
		
		tank.addChild(laser,100);
	}));
	return getTankRunAction(tank);
}

function createSmallExp(x,y){
	var exp = cc.ParticleSystem.createWithTotalParticles(10);
	exp.setPosition(x,y);
	exp.setTexture(cc.TextureCache.getInstance().addImage(R_EXPLOSION_PTC));
	exp.setAutoRemoveOnFinish(true);
    // duration
	exp.setDuration(0.4);

    // angle
	exp.setAngle(0);
	exp.setAngleVar(180);

    // speed of particles
	exp.setSpeed(10);
	exp.setPosVar(cc.p(10, 10));

    // life of particles
	exp.setLife(0.3);

    // spin of particles
	exp.setStartSpin(0);
	exp.setStartSpinVar(180);
	exp.setEndSpin(0);
	exp.setEndSpinVar(0);

	exp.setStartColor(cc.c4f(1, 1, 0, 0.8));
	exp.setStartColorVar(cc.c4f(0, 0, 0, 0));
	exp.setEndColor(cc.c4f(1, 1, 0, 1));
	exp.setEndColorVar(cc.c4f(0, 0, 0, 0));

    // size, in pixels
	exp.setStartSize(10.0);
	exp.setEndSize(30);

    // emits per second
	exp.setEmissionRate(10);

    // additive
	exp.setBlendAdditive(false);
	var parent = TW.gameScn.tankLayer.effect;
	parent.addChild(exp);
	return exp;
}

function createExplose(x,y){
	var exp = cc.ParticleSystem.createWithTotalParticles(20);
	exp.setPosition(grid2pos(x,y));
	exp.setTexture(cc.TextureCache.getInstance().addImage(R_EXPLOSION_PTC));
	exp.setAutoRemoveOnFinish(true);
    // duration
	exp.setDuration(0.4);

    // angle
	exp.setAngle(0);
	exp.setAngleVar(180);

    // speed of particles
	exp.setSpeed(20);
	exp.setSpeedVar(10);

	exp.setPosVar(cc.p(20, 20));

    // life of particles
	exp.setLife(0.3);
	exp.setLifeVar(0.1);

    // spin of particles
	exp.setStartSpin(0);
	exp.setStartSpinVar(180);
	exp.setEndSpin(0);
	exp.setEndSpinVar(0);

	exp.setStartColor(cc.c4f(0.5, 0, 0, 0.8));
	exp.setStartColorVar(cc.c4f(0, 0, 0, 0));
	exp.setEndColor(cc.c4f(1, 1, 1, 1));
	exp.setEndColorVar(cc.c4f(0, 0, 0, 0));

    // size, in pixels
	exp.setStartSize(40.0);
	exp.setStartSizeVar(10);
	exp.setEndSize(100);

    // emits per second
	exp.setEmissionRate(exp.getTotalParticles() / exp.getLife());

    // additive
	exp.setBlendAdditive(false);
	var parent = TW.gameScn.tankLayer.effect;
	parent.addChild(exp);
	return exp;
}

function createHealth(x,y){
	var eff = cc.ParticleSystem.createWithTotalParticles(20);
	eff.setPosition(grid2pos(x,y));
	eff.setTexture(cc.TextureCache.getInstance().addImage(R_HP_PTC));
	eff.setAutoRemoveOnFinish(true);	
	
	eff.setDuration(3);
	eff.setPosVar(cc.p(10,10));
	eff.setLife(0.8);
	eff.setLifeVar(0.1);
	eff.setAngle(90);
	eff.setAngleVar(10);
	eff.setSpeed(100);
	eff.setSpeedVar(10);
	eff.setStartSize(30);
	eff.setStartSizeVar(5);
	eff.setEndSize(50);
	eff.setEmissionRate(4);
	
	eff.setStartColor(cc.c4f(0.1568,1,0.0431,1));
	eff.setStartColorVar(cc.c4f(0,0,0,0));
	eff.setEndColor(cc.c4f(0.1568,1,0.0431,0.8));
	eff.setEndColorVar(cc.c4f(0,0,0,0));
	
	var parent = TW.gameScn.tankLayer.effect;
	parent.addChild(eff);	
	return eff;
}

function createSleep(i){
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	var eff = cc.ParticleSystem.createWithTotalParticles(30);
	eff.setPosition(tank.getPositionShade());
	eff.setTexture(cc.TextureCache.getInstance().addImage(R_SLEEP));
	eff.setAutoRemoveOnFinish(true);	
	
	eff.setDuration(-1);
	eff.setLife(2);
	eff.setLifeVar(0.2);
	eff.setAngle(80);
	eff.setAngleVar(0);
	eff.setSpeed(30);
	eff.setSpeedVar(10);
	eff.setRadialAccel(15);
	eff.setStartSize(5);
	eff.setStartSizeVar(5);
	eff.setEndSize(40);
	eff.setEmissionRate(1);
	eff.setStartColor(cc.c4f(102./255.,153./255.,204./255.,1));
	eff.setStartColorVar(cc.c4f(0,0,0,0));
	eff.setEndColor(cc.c4f(102./255.,153./255.,204./255.,1));
	eff.setEndColorVar(cc.c4f(0,0,0,0));
	
	if(tank.sleep){
		tank.sleep.removeFromParent(true);
	}
	
	var parent = TW.gameScn.tankLayer.effect;
	parent.addChild(eff);
	tank.sleep = eff;
	return eff;
}

function pickItem(x, y, card, file, num){
	var item = null;
	if(card){
		item = cc.Sprite.create(R_ALL_CARD, cardRect(card));
		item.setScale(0.5);
	}
	else{
		item = cc.Sprite.create(file);
	}
	if(num){
		var label = cc.LabelTTF.create('+'+num, S_TIP_FONT, 24);
		var sz = item.getTextureRect().size;
		label.setPositionX(sz.width-30);
		label.setColor(cc.c3b(1,74,224));
		var node = cc.Node.create();
		node.addChild(item);
		node.addChild(label);
		item = node;
	}
	
	item.setPosition(grid2pos(x,y));	
	item.setScale(0.3,0.3);
	var parent = TW.gameScn.tankLayer.effect;
	parent.addChild(item);
	
	var mov = cc.MoveBy.create(3,cc.p(0,50));
	var scale = cc.ScaleTo.create(1.5, 2, 2);
	var rm = cc.CallFunc.create(function(){item.removeFromParent(true);});
	item.runAction(cc.Sequence.create(cc.Spawn.create(mov,scale),rm));
		
	return item;
}

function createShell(i){
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	var shell = cc.Sprite.create(R_SHELL);
	adjustSizeForGrid(shell);
	tank.addChild(shell, 10);
	if(tank.shell){
		tank.shell.removeFromParent(true);
	}
	tank.shell = shell;
	return tank;
}

function createFire(i){
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	if(tank.fire){
		tank.fire.removeFromParent(true);
	}
	var fire = cc.ParticleFire.create();
	fire.setScale(0.4);
	fire.setPosition(cc.p(0,-40));
	fire.setAngle(-90);
	tank.addChild(fire, 9);
	tank.fire = fire;
	return tank;
}

function fireAround(i){
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	if(tank.hat.getRotation()!=0){
		tank.hat.runAction(cc.RotateTo.create(0.2,0));
	}
	
	addTankAction(tank, cc.CallFunc.create(function(){
		var fire = cc.ParticleFire.create();
		fire.setAutoRemoveOnFinish(true);		
		fire.setDuration(4);
		fire.setPositionType(cc.PARTICLE_TYPE_FREE);		
		
		fire.setScale(0.4);
		fire.setPosition(cc.p(0,40));
		fire.setAngle(90);
		fire.setSpeed(300);
		fire.setSpeedVar(50);
		tank.addChild(fire, 9);
	}));
	addTankAction(tank, cc.DelayTime.create(1));
	addTankAction(tank, cc.RotateBy.create(3,360*3));
	return getTankRunAction(tank);	
}

function createHeli(){
	var hel = cc.Sprite.create(R_HELI);
	hel.setPosition(cc.p(0,100));
	hel.setRotation(90);
	var parent = TW.gameScn.tankLayer.effect;
	parent.addChild(hel);
	
	hel.runAction(cc.RepeatForever.create(cc.Animate.create(loadAnim(R_HELI_ANIM))));
	var mov = cc.MoveTo.create(3,cc.p(900,100));
	var rm = cc.CallFunc.create(function(){hel.removeFromParent(true);});
	hel.runAction(cc.Sequence.create(mov,rm));
	return hel;
}

function createFlag(i,x,y){
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	var stepx = Math.abs(tank.player.pos.x - x);
	var stepy = Math.abs(tank.player.pos.y - y);
	if(stepx > tank.player.speed || stepy > tank.player.speed){
		return;
	}
	
	var parent = TW.gameScn.uiLayer.flagParent;
	var flag = parent.getChildByTag(TAG_FLAG);
	if(!flag){
		flag = cc.Sprite.create(R_FLAG);
		parent.addChild(flag, 0, TAG_FLAG);		
		adjustSizeForGrid(flag);
	}
	flag.setAnchorPoint(cc.p(-0.2,-0.3));
	flag.setPosition(grid2pos(x,y));
	return flag;
}

var TAG_MAREA = 10;
function clamp(x,m){
	if(x<0){return 0;}
	if(x>=m){return m-1;}
	return x;
}
function showMoveArea(i, show){
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	var player = tank.player;
	
	var bg = TW.gameScn.bgLayer;
	var area = bg.getChildByTag(TAG_MAREA);
	if(show){
		if(!area){
			var res = cc.SpriteBatchNode.create(R_MOVE_AREA);
			area = cc.Scale9Sprite.create();
			area.updateWithBatchNode(res, cc.rect(0,0,32,32), false, cc.rect(10,10,10,10));
			area.setAnchorPoint(cc.p(0,0));
			bg.addChild(area, 10, TAG_MAREA);
		}
		var x1 = clamp(player.pos.x - player.speed, TW.room_w);
		var x2 = clamp(player.pos.x + player.speed, TW.room_w);
		var y1 = clamp(player.pos.y - player.speed, TW.room_h);
		var y2 = clamp(player.pos.y + player.speed, TW.room_h);
		var pos = grid2pos(x1,y1);
		var sz = grid2pos(x2-x1+1, y2-y1+1);
		area.setContentSize(cc.size(sz.x, sz.y));
		area.setPosition(cc.p(MAP_OFFSET.x+pos.x, MAP_OFFSET.y+pos.y));
	}
	else{
		if(area){
			area.removeFromParent(true);
		}
	}
	return area;
}

function showWinnerDlg(name){
	var sz = cc.Director.getInstance().getWinSize();
	var res = cc.SpriteBatchNode.create(R_DLG_PIC);
	var dlg = cc.Scale9Sprite.create();
	dlg.updateWithBatchNode(res, cc.rect(0,0,32,32), false, cc.rect(10,10,10,10));
	dlg.setContentSize(cc.size(400,300));
	dlg.setPosition(sz.width/2, sz.height/2);
	
	var lb = cc.LabelTTF.create(S_WINNER_LB, S_NAME_FONT, 32);
	lb.setPosition(cc.p(100,250));
	dlg.addChild(lb);
	var name = cc.LabelTTF.create(name, S_NAME_FONT, 48);
	name.setPosition(cc.p(200,200));
	name.setColor(cc.c3b(251,139,0));
	dlg.addChild(name);
	
		cc.MenuItemFont.setFontName(S_MENU_FONT);
		cc.MenuItemFont.setFontSize(32);
		var quit = cc.MenuItemFont.create(S_MENU_OK, TW.gameScn.uiLayer.quitPlay, TW.gameScn.uiLayer);
		quit.setColor(cc.c3b(200,0,0));
		var menu = cc.Menu.create(quit);
		menu.setPosition(cc.p(0,0));
		dlg.addChild(menu, 1);	
		quit.setPosition(cc.p(350,50));
	
	TW.gameScn.uiLayer.addChild(dlg, 100);
}

function mouse2grid(pos){
    var parent = TW.gameScn.uiLayer.flagParent;
	var pos2 = parent.convertToNodeSpace(pos);
	var x = Math.floor(pos2.x/GRID_SIZE);
	var y = Math.floor(pos2.y/GRID_SIZE);
	if(x<0 || x>=TW.room_w || y<0 || y>=TW.room_h){
		return;
	}
	return cc.p(x,y);
}

function createConflict(x,y,tanks){
	var eff = cc.ParticleGalaxy.create();
	eff.setPosition(grid2pos(x,y));
	eff.setDuration(3);
	eff.setAutoRemoveOnFinish(true);
	var parent = TW.gameScn.tankLayer.effect;
	parent.addChild(eff);
	
	function shade(tank, n){
		if(n>0){
			var x1 = (Math.random()-0.5)*GRID_SIZE;
			var y1 = (Math.random()-0.5)*GRID_SIZE;
			var mov = cc.MoveTo.create(0.1, cc.p(x1, y1));
			addTankAction(tank, mov);	
			shade(tank, n-1);
		}
		else{
			addTankAction(tank, cc.MoveTo.create(0.1,cc.PointZero()));
			getTankRunAction(tank)();
		}
	}
	tanks.forEach(function(id){
		var tank = TW.tanks[id];
		if(tank){
			tank.shade.setPosition(grid2pos(x,y));
			tank.runAction(cc.RotateBy.create(1.5, 360*4));
			shade(tank, 20);
		}
	});
	
	return eff;
}

function chgHP(i,newHP){
	var tank = TW.tanks[i];
	if(!tank){
		return;
	}
	
	var hp = newHP - tank.player.hp;
	tank.player.hp = newHP;
	if(hp!=0){
		var label;
		if(hp>0){
			label = cc.LabelTTF.create('+'+hp, S_TIP_FONT, 32);
			label.setColor(cc.c3b(77,230,0));
		}
		else{
			label = cc.LabelTTF.create(''+hp, S_TIP_FONT, 32);
			label.setColor(cc.c3b(255,28,28));
		}
		var pos = tank.getPositionShade();
		label.setPosition(cc.p(pos.x+GRID_SIZE/2, pos.y));
		var parent = TW.gameScn.tankLayer.effect;
		parent.addChild(label);
		
		label.runAction(cc.MoveBy.create(3,cc.p(0,60)));
		var fade = cc.FadeTo.create(3, 200);
		var rm = cc.CallFunc.create(function(){label.removeFromParent(true);});
		label.runAction(cc.Sequence.create(fade,rm));
	}

	if (newHP > 0 && hp != 0) {
	    var percent = 100 * newHP / tank.player.maxHP;
	    tank.hpBar.stopAllActions();
	    tank.hpBar.runAction(cc.ProgressFromTo.create(0.5, tank.hpBar.getPercentage(),percent));
		updateHPBoard(tank.player.name, percent);
	}

	if(newHP<=0){
		createExplose(tank.player.pos.x, tank.player.pos.y);
		tank.shade.removeFromParent(true);
		delete TW.tanks[i];
	}
}

function loadAnim(res){
	var frms = [];
	var num = res.num;
	var wsz = res.split[0];
	var wn = res.split[1];
	var hsz = res.split[2];
	var hn = res.split[3];
	for(var h=0;h<hn; ++h){
		for(var w=0; w<wn; ++w){
			if(num>0){
				num -= 1;
				var frm = cc.SpriteFrame.create(res.file, new cc.Rect(w*wsz,h*hsz,wsz,hsz),true,
				cc.p(0.5,0.5), cc.size(wsz,hsz));
				frms.push(frm);
			}
		}
	}
	
	return cc.Animation.create(frms, res.step);
}

var TAG_TIMER = 101;
function showMoveTime(L){
	var sz = cc.Director.getInstance().getWinSize();
    var hp = cc.ProgressTimer.create(cc.Sprite.create(R_HP_STATUS));
    hp.setType(cc.PROGRESS_TIMER_TYPE_BAR);
    hp.setMidpoint(cc.p(0, 0));
    hp.setBarChangeRate(cc.p(0, 1));
    hp.setPercentage(100);
    hp.setPosition(cc.p(20, sz.height/2));
    hp.setColor(cc.c3b(102, 0, 204));
	hp.setScale(0.5,19.4);
    TW.gameScn.bgLayer.addChild(hp, 0, TAG_TIMER);
	var pg = cc.ProgressFromTo.create(L, 100,0);
	var rm = cc.CallFunc.create(function(){hp.removeFromParent(true);});
	hp.runAction(cc.Sequence.create(pg,rm));
}

function updateHPBoard(name, hp){
	TW.uiBoards = TW.uiBoards || {cnt:0};
	var tag = TW.uiBoards[name];
	if(!tag){
		tag = cc.Sprite.create(R_UI_TAG);
		TW.uiBoards[name] = tag;
		tag.cnt = TW.uiBoards.cnt;
		TW.uiBoards.cnt += 1;
				
		var lb = cc.LabelTTF.create(name, S_NAME_FONT, 30);
		lb.setPosition(cc.p(20,150));
		lb.setColor(cc.c3b(0,0,0));
		lb.setAnchorPoint(cc.PointZero());
		tag.addChild(lb);

		var hp = cc.ProgressTimer.create(cc.Sprite.create(R_HP_STATUS));
		hp.setType(cc.PROGRESS_TIMER_TYPE_BAR);
		hp.setMidpoint(cc.p(0, 0));
		hp.setBarChangeRate(cc.p(1, 0));
		hp.setPercentage(100);
		hp.setAnchorPoint(cc.PointZero());
		hp.setPosition(cc.p(95, 150));
		hp.setColor(cc.c3b(0,255,0));
		hp.setScale(2.5,0.1);
		tag.addChild(hp);
		tag.hp = hp;
	
		tag.setScale(0.8,0.8);
		var sz = cc.Director.getInstance().getWinSize();
		var x = tag.cnt * 170 + 120;
		var y = sz.height - 75 - 5*Math.random();
		tag.setPosition(cc.p(x,y));
		
		TW.gameScn.uiLayer.addChild(tag);	
	}
	
	if(tag.hp.getPercentage()!=hp){
		tag.hp.runAction(cc.ProgressFromTo.create(0.5, tag.hp.getPercentage(),hp));
	}
	return tag;
}

function updateBuffBoard(name,buf,cnt){
	var tag = TW.uiBoards[name];
	tag.bufs = tag.bufs || [];
	for(var i in tag.bufs){
		var bufctrl = tag.bufs[i];
		if(bufctrl.bufid==buf && !bufctrl.empty){
			bufctrl.lb.setString(cnt);
			if(cnt==0){
				bufctrl.removeFromParent(true);
				bufctrl.empty = true;				
			}
			return bufctrl;			
		}
	}
	if(cnt==0){
		return;
	}
	
	var pos = tag.bufs.length;
	for(var i in tag.bufs){
		var bufctrl = tag.bufs[i];
		if(bufctrl.empty){
			pos = i;
			break;
		}
	}
	
	var bufctrl = cc.Node.create();
	var icon = cc.Sprite.create(R_ALL_CARD, cardRect(buf));
	icon.setScale(0.5,0.5);
	icon.setAnchorPoint(cc.PointZero());
	bufctrl.icon = icon;
	bufctrl.addChild(icon);
	
	var lb = cc.LabelTTF.create(cnt, S_NAME_FONT, 30);
	lb.setColor(cc.c3b(0,0,0));
	lb.setPosition(30,-5);
	lb.setAnchorPoint(cc.PointZero());
	bufctrl.addChild(lb);
	bufctrl.lb = lb;
	bufctrl.empty = false;
	bufctrl.bufid = buf;
	tag.bufs[pos] = bufctrl;
	
	var x = Math.floor(pos/3);
	var y = pos%3;
	bufctrl.setPosition(15+x*60, 110-y*35);
	tag.addChild(bufctrl);
	return bufctrl;
}